package com.ruoyi.utils;


import com.ruoyi.common.utils.DatabaseUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SequenceBuilder;
import com.ruoyi.dm.domain.DataCollectionParam;
import com.ruoyi.dm.mapper.DataCollectionMapper;
import com.ruoyi.dm.service.impl.DataCollectionServiceImpl;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;

/**
 * 时间工具类
 * 
 * @author zz
 */
@Component
public class DealDataUtils {

    @Resource
    DataCollectionMapper dataCollectionMapper;

    public static DealDataUtils dealDataUtils;

    @PostConstruct
    public void init(){
        dealDataUtils=this;
    }

    //国外卫星大气密度数据处理
    public Map<String,Object> dealForeignDensity(File folder,Integer nums){
        Map<String, Object> resMap = new HashMap<>();

        if (!folder.isDirectory()) {
            int count = 0;
            Date date = new Date();
            List<Map<String, Object>> listData = new ArrayList<>();
            //进行文件读取
            try {

                InputStreamReader read = new InputStreamReader(new FileInputStream(folder), "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(read);
                String str = null;
                bufferedReader.readLine();
                while ((str = bufferedReader.readLine()) != null){
                    Map<String, Object> map = new HashMap<>();
                    String[] args = str.split("\\t");
                    map.put("TIME",args[0]);
                    map.put("ALT",args[1]);
                    map.put("LON",args[2]);
                    map.put("LAT",args[3]);
                    map.put("DENSITY",args[4]);
                    map.put("CREATE_TIME",date);
                    map.put("ID", SequenceBuilder.sequenceId());
                    listData.add(map);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //进行文件索引数据入库保存
            List<DataCollectionParam> listMap = new ArrayList<>();
            DataCollectionParam param = new DataCollectionParam();
            param.setFileName(folder.getName());
            param.setOrFileName(folder.getName());
            param.setFilePath(folder.getPath());
            param.setFileType(folder.getName().substring(folder.getName().lastIndexOf(".")).split("\\.")[1]);
            param.setFileSize(new BigDecimal((double) folder.length() / (1024 * 1024)).setScale(2, RoundingMode.UP) + "MB");
            param.setStartTime(DateUtils.convertString2Date("yyyy-MM-dd HH:mm:ss",listData.get(0).get("TIME")+""));
            param.setEndTime(DateUtils.convertString2Date("yyyy-MM-dd HH:mm:ss",listData.get(listMap.size()-1).get("TIME")+""));
            param.setCreateTime(date);
            param.setId(SequenceBuilder.sequenceId());
            String tableName = "";
            switch (folder.getParentFile().getName().toUpperCase()){
                case "STANDARD_DEN_CHAMP" :
                    param.setDataSource("CHAMP卫星");
                    break;
                case "STANDARD_DEN_FO" :
                    param.setDataSource("GRACE-FO卫星");
                    break;
                case "STANDARD_DEN_GA" :
                    param.setDataSource("GRACE-A卫星");
                    break;
                case "STANDARD_DEN_GB" :
                    param.setDataSource("GRACE-B卫星");
                    break;
                case "STANDARD_DEN_GO" :
                    param.setDataSource("GRACE-GOES卫星");
                    break;
                case "STANDARD_DEN_SA" :
                    param.setDataSource("SWARM-A卫星");
                    tableName = "SWDATA_OBS_DEN_SWARMA";
                    break;
                case "STANDARD_DEN_SB" :
                    param.setDataSource("SWARM-B卫星");
                    tableName = "SWDATA_OBS_DEN_SWARMB";
                    break;
                case "STANDARD_DEN_SC" :
                    param.setDataSource("SWARM-C卫星");
                    tableName = "SWDATA_OBS_DEN_SWARMC";
                    break;
            }
            listMap.add(param);
            String aa = tableName;
            dealDataUtils.dataCollectionMapper.add(listMap,tableName+"_FILE");
            Function<List<?>,Integer> function = x->dealDataUtils.dataCollectionMapper.inserBatch((List<Map<String,Object>>)x,(aa));
            count+= DatabaseUtil.splitInsert(listData, function, nums);
            return null;
        }

        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    dealForeignDensity(file,nums); // 递归调用
                } else {
                    int count = 0;
                    List<Map<String, Object>> listData = new ArrayList<>();
                    //进行文件读取
                    Date date = new Date();
                    try {
                        String tableName = file.getParentFile().getName();
                        //进行文件信息添加
                        InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                        BufferedReader bufferedReader = new BufferedReader(read);
                        String str = null;
                        bufferedReader.readLine();
                        while ((str = bufferedReader.readLine()) != null){
                            Map<String, Object> map = new HashMap<>();
                            String[] args = str.split("   ");
                            map.put("TIME",args[0]);
                            map.put("ALT",args[1]);
                            map.put("LON",args[2]);
                            map.put("LAT",args[3]);
                            map.put("DENSITY",args[4]);
                            map.put("CREATE_TIME",date);
                            map.put("ID", SequenceBuilder.sequenceId());
                            listData.add(map);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    //进行文件索引数据入库保存
                    List<DataCollectionParam> listMap = new ArrayList<>();
                    DataCollectionParam param = new DataCollectionParam();
                    param.setFileName(file.getName());
                    param.setOrFileName(file.getName());
                    param.setFilePath(file.getPath());
                    param.setFileType(file.getName().substring(file.getName().lastIndexOf(".")).split("\\.")[1]);
                    param.setFileSize(new BigDecimal((double) file.length() / (1024 * 1024)).setScale(2, RoundingMode.UP) + "MB");
                    param.setStartTime(DateUtils.convertString2Date("yyyy-MM-dd HH:mm:ss",listData.get(0).get("TIME")+""));
                    param.setEndTime(DateUtils.convertString2Date("yyyy-MM-dd HH:mm:ss",listData.get(listData.size()-1).get("TIME")+""));
                    param.setCreateTime(date);
                    param.setFrequency("10s");
                    param.setId(SequenceBuilder.sequenceId());
                    String tableName = "";
                    switch (file.getParentFile().getName().toUpperCase()){
                        case "STANDARD_DEN_CHAMP" :
                            param.setDataSource("CHAMP卫星");
                            break;
                        case "STANDARD_DEN_FO" :
                            param.setDataSource("GRACE-FO卫星");
                            break;
                        case "STANDARD_DEN_GA" :
                            param.setDataSource("GRACE-A卫星");
                            break;
                        case "STANDARD_DEN_GB" :
                            param.setDataSource("GRACE-B卫星");
                            break;
                        case "STANDARD_DEN_GO" :
                            param.setDataSource("GRACE-GOES卫星");
                            break;
                        case "STANDARD_DEN_SA" :
                            param.setDataSource("SWARM-A卫星");
                            tableName = "SWDATA_OBS_DEN_SWARMA";
                            break;
                        case "STANDARD_DEN_SB" :
                            param.setDataSource("SWARM-B卫星");
                            tableName = "SWDATA_OBS_DEN_SWARMB";
                            break;
                        case "STANDARD_DEN_SC" :
                            param.setDataSource("SWARM-C卫星");
                            tableName = "SWDATA_OBS_DEN_SWARMC";
                            break;
                    }
                    listMap.add(param);
                    String aa = tableName;
                    dealDataUtils.dataCollectionMapper.add(listMap,tableName+"_FILE");
                    Function<List<?>,Integer> function = x->dealDataUtils.dataCollectionMapper.inserBatch((List<Map<String,Object>>)x,(aa));
                    count+= DatabaseUtil.splitInsert(listData, function, nums);
                }
            }
        }
        return null;


    }


    public void dealDcData(File folder, Integer nums) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    dealForeignDensity(file,nums); // 递归调用
                } else {
                    int count = 0;
                    List<Map<String, Object>> listData = new ArrayList<>();
                    //进行文件读取
                    Date date = new Date();
                    try {
                        String tableName = file.getParentFile().getName();
                        //进行文件信息添加
                        InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                        BufferedReader bufferedReader = new BufferedReader(read);
                        String str = null;
                        bufferedReader.readLine();
                        while ((str = bufferedReader.readLine()) != null){
                            Map<String, Object> map = new HashMap<>();
                            String[] args = str.split(",");
                            map.put("DATETIME",args[0]);
                            map.put("Y107",args[1]);
                            listData.add(map);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Function<List<?>,Integer> function = x->dealDataUtils.dataCollectionMapper.insertDcBatch((List<Map<String,Object>>)x,("SWDATA_OBS_Y107"));
                    count+= DatabaseUtil.splitInsert(listData, function, nums);
                }
            }
        }
    }
    public static void deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
            System.gc();
        }
    }
}
